import axios from '@/utils/request'
export default {
  TagCategory: {
    entity: { name: 'Category', prefix: `manage` },
    form: {
      fields: [
        'name',
        {
          property: 'type',
          relation_filter: {
            '@filter': 'entity.getSlug() == "tagCategory"'
          }
        },
        {
          property: 'parent',
          required: false,
          relation_filter: {
            '@filter': `entity.getType().getSlug() == 'ProductCategory' && !entity.getParent()`
          }
        },
        { property: 'enabled', required: false, default_value: true },
        { property: 'sequence', required: false, default_value: 0 },
      ]
    },
    list: {
      query: {
        '@filter': `entity.getType().getSlug() == 'tagCategory'`
      },
      list_display: ['id', 'type', 'name', 'parent', 'enabled', 'sequence'],
      list_filter: {
        'parent.id': () => {
          return axios
            .get('/manage/categories', { params: { '@filter': `entity.getType().getSlug() == 'ProductCategory' && !entity.getParent()` } })
            .then(res =>
              Object.assign({ __label: '产品分类' },
                ...res.data.map(v => { return { [v.id]: v.name } })
              )
            )
        }
      }
    }
  }
}
